# 连接数据库
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.pool import NullPool

class DbHandler(object):
    def __init__(self, name='lhjy'):
        con_url = 'mysql+pymysql://root:alpine@localhost:3306/' + name
        self.db_addr = con_url

    def create_session(self):
        # 初始化数据库连接:
        # engine = create_engine(con_url, pool_size=100, pool_recycle=3600)
        self.engine = create_engine(self.db_addr, poolclass=NullPool)
        self.session = scoped_session(sessionmaker(bind=self.engine))

    def close_session(self):
        self.session.close()
db = DbHandler()
db.create_session()